package work.xkong.advice;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import work.xkong.common.ResponseRest;
import work.xkong.entity.Student;
import work.xkong.entity.Teacher;
import work.xkong.mapper.StudentMapper;
import work.xkong.mapper.TeacherMapper;
import work.xkong.vo.inputvo.CourseSelectInputVo;
import work.xkong.vo.outputvo.CourseSelectOutputVo;

import java.util.List;

/**
 * @PACKAGE_NAME: work.xkong.advice
 * @NAME: AboutStudentCourseAop
 * @USER: NuanYang
 * @author: 查炼飞
 * @create: 2022/11/26 12:44
 */
@Aspect
@Component
public class AboutStudentCourseAop {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private TeacherMapper teacherMapper;

    @Pointcut("execution(* work.xkong.service.impl.CourseServiceImpl.get*Course(..))")
    public void s() {
    }

    @Pointcut("execution(* work.xkong.service.impl.CourseServiceImpl.get*Courses(..))")
    public void t() {
    }

    @Around(value = "s()")
    public ResponseRest getCourse(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        CourseSelectInputVo inputVo = (CourseSelectInputVo) args[0];

        //判断学生是否存在
        Student student = studentMapper.selectOne(new LambdaQueryWrapper<Student>().eq(Student::getStudentId, inputVo.getId()));
        if (student == null) {
            return ResponseRest.creatFail("该学员不存在，非法请求");
        }

        if (inputVo.getCurrentPage() == null) {
            inputVo.setCurrentPage(1);
        }

        if (inputVo.getPageSize() == null) {
            inputVo.setPageSize(8);
        }

        return (ResponseRest) joinPoint.proceed();
    }

    @Around(value = "t()")
    public ResponseRest getCourses(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        CourseSelectInputVo inputVo = (CourseSelectInputVo) args[0];

        //判断学生是否存在
        Teacher teacher = teacherMapper.selectOne(new LambdaQueryWrapper<Teacher>().eq(Teacher::getTeacherId, inputVo.getId()));
        if (teacher == null) {
            return ResponseRest.creatFail("该老师不存在，非法请求");
        }

        if (inputVo.getCurrentPage() == null) {
            inputVo.setCurrentPage(1);
        }

        if (inputVo.getPageSize() == null) {
            inputVo.setPageSize(8);
        }

        return (ResponseRest) joinPoint.proceed();
    }

}
